# -*- coding: utf-8 -*-
"""
Created on Thu Jun  1 14:23:10 2023

@author: ZHOU
"""
import time

'''data keywords
心率 "heartrate"
踏频 "cadence"
距离 "distance"
功率 "power"
'''

def trans_time(st):
    if st<10 and st>=0:
        st="0"+str(st)
    else:
        st=str(st)
    return str(st)
    
def gpx_to_srt(gpx):     
    keywords_list=["heartrate","cadence","power","distance"]
    srt_list=[]
    first_time=0
    srt_time=0
    srt=""
    j=1
    trkpt_flag=0
    trkpt_first_flag=0
    for i in range(len(gpx)):        
        try:
            ti=str((gpx[i].split("<time>")[1]).split("</time>")[0])
            now_time = time.mktime(time.strptime(ti, "%Y-%m-%dT%H:%M:%SZ"))
            first_time=now_time                
            srt_time=0
            str_now_time=time.strftime("<i>%Y-%m-%d %H:%M:%S</i>\n",time.localtime(now_time))  
            srt=str(j)+"\n00:00:00,000 --> 00:00:01,000\n"+str_now_time+"<u>BEGIN</u>\n\n"
            srt_list.append(srt)
            break
        except:
            pass
    
    srt=""
    srt_str_time=""
    last_distance=0
    srt_time=0
    last_srt_time=0
    for i in range(len(gpx)):
        if gpx[i].count('<trkpt'):
            trkpt_flag=1
            trkpt_first_flag=1
            j=j+1

        if trkpt_flag==1:
            try:
                ti=str((gpx[i].split("<time>")[1]).split("</time>")[0])
                now_time = time.mktime(time.strptime(ti, "%Y-%m-%dT%H:%M:%SZ"))                
                srt_time=now_time-first_time
                
                h=int(srt_time/3600)
                m=int(srt_time/60)
                s=int(srt_time%60)
                s2=s+1
                h=trans_time(h)
                m=trans_time(m) 
                s=trans_time(s)
                s2=trans_time(s2)
                srt_str_time=str(h+":"+m+":"+s+",000 --> "+h+":"+m+":"+s2+",000\n")
                str_now_time=time.strftime("<i>%Y-%m-%d %H:%M:%S</i>\n",time.localtime(now_time))  
                srt=srt+str_now_time
            except:
                pass
            
            if gpx[i].count('<trkpt'):
                srt=srt+"lat:"+gpx[i].split('"')[1]+" lon:"+gpx[i].split('"')[3]+"\n"

            try:
                el=str((gpx[i].split("<ele>")[1]).split("</ele>")[0]) 
                srt=srt+"ele:"+el+"\n"
            except:
                pass
            
            for key in keywords_list:                
                try:
                    dat=str((gpx[i].split("<"+key+">")[1]).split("</"+key+">")[0]) 
                    srt=srt+"<i>"+key+":"+dat+"</i>\n"
                    if key=="distance":    
                        velocity_time=srt_time-last_srt_time
                        velocity=(int(dat)-last_distance)/velocity_time*3.6                        
                        srt=srt+"<b>"+"velocity"+":"+str(velocity)+"</b>\n"                        
                except:
                    pass
        
        if gpx[i].count('</trkpt>'):
            trkpt_flag=0
        
        if trkpt_first_flag==1 and trkpt_flag==0:
            if not srt=="":                
                last_srt_time=int(srt_time)
                last_distance=int(dat)
                srt=str(j)+"\n"+srt_str_time+srt+"\n"
                srt_list.append(srt)
                srt=""
        
        if gpx[i].count('</trkseg>'):
            j=j+1
            trkpt_first_flag=2
            
        if trkpt_first_flag==2:
            h=int((last_srt_time+1)/3600)
            m=int((last_srt_time+1)/60)
            s=int((last_srt_time+1)%60)
            s2=s+1
            h=trans_time(h)
            m=trans_time(m) 
            s=trans_time(s)
            s2=trans_time(s2)
            srt_str_time=str(h+":"+m+":"+s+",000 --> "+h+":"+m+":"+s2+",000\n")
            srt=str(j)+"\n"+srt_str_time+"<u>END</u>\n"+"\n"
            srt_list.append(srt)
            srt=""
            break
   
    return srt_list

def save_lines(lines,path):
    try:
        f=open(path, 'w', encoding="utf-8")
    except:
        f=open(path, 'a', encoding="utf-8")
    for i in lines:
        f.write(i)
    f.close()
    
if __name__ == '__main__':
    path="./1.gpx"
    
    f=open(path, 'r', encoding="utf-8")
    gpx=f.readlines()
    f.close()    
    
    gpx=gpx_to_srt(gpx)

    save_lines(gpx,"srt_test.srt")
